home *** CD-ROM | disk | FTP | other *** search
/ BMUG PD-ROM A / PD-ROM A.iso / Education / Math / Matrix.0 / Read⁄Write Source / read binary.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-07-12  |  3.1 KB  |  136 lines  |  [TEXT/KAHL]

  1. /*
  2.     READ BINARY.c
  3.     
  4.     by:      John A. Schlack
  5.     Date:    July 12, 1990
  6.     
  7.         This is a procedure designed to help one create the matrix files.  It is
  8.     taken from the source code for Matrix Manipulation with certain procedures
  9.     commented out.  This should help one visualize how the data should be saved
  10.     as this is the procedure that Matrix Manipulation uses to read the matrix
  11.     data file.
  12.         If you want to know how to read a binary file, see WRITE BINARY.c.
  13.     You can modify these two files to read and write ASCII files.  These are
  14.     only meant as a guide line, and are not meant to be simply placed into
  15.     a program to create and read the Matrix Manipulation files.
  16. */
  17.  
  18.  
  19. extern struct filePreferences
  20. {
  21.     Boolean        matrixSizeInFile;
  22. } gFilePrefs;
  23.  
  24. extern    double    **gA;
  25. extern    double    **gY;
  26. extern     int        gN;
  27.  
  28.  
  29. Boolean ReadBinaryFileData( reply )
  30.  
  31. SFReply reply;
  32.  
  33. /*
  34.     gN is the size of the matrix (N x N)
  35.     gA is a global double ** that stores the matrix (it is an array [0..N][0..N])
  36.     gY tracks the previous values of gA;  it is initially set to gA
  37.     gFilePrefs.matrixSizeInFile stores a flag which states whether the matrix size
  38.             is in the file or will be entered by hand
  39.     
  40.     PROCEDURE:
  41.     1.    open the file
  42.     2.    set file position to beginning of file
  43.     3.    if matrix size in file, read the size and check to see if in bounds (2 to 99)
  44.             else, call procedure to get size input from user
  45.     4.    InitGlobalPointers is commented out;  this allocates storage and initializes
  46.             gA, gY, and the vectors;  be sure that you have allocated storage to save
  47.             the values that are read in
  48.     5.    enter a loop which reads a double precision value and saves it in gA and gY
  49.     6.    if an error occurred, release storage space, call a procedure to display an
  50.             error alert (fileErrors or theErrorHandler), and break out of reading loop
  51.     7.    close file
  52.     8.    return a Boolean value (TRUE if an error, FALSE if no error)
  53. */
  54.  
  55. {
  56.     int         srcFile;
  57.     register int     i;
  58.     register int     j;
  59.     long         siz;
  60.     OSErr         err;
  61.     double         c;
  62.     Boolean     theErr = FALSE;
  63.     Boolean        goOn = TRUE;
  64.  
  65.     err = FSOpen( reply.fName, reply.vRefNum, &srcFile );
  66.     if (err == noErr)
  67.     {
  68.         SetFPos( srcFile, fsFromStart, 0L );
  69.         if (gFilePrefs.matrixSizeInFile == TRUE)
  70.         {
  71.             siz = (long) sizeof(double);
  72.             err = FSRead( srcFile, &siz, &c );
  73.             if (err != noErr)
  74.             {
  75. /*                fileErrors( err );*/
  76.                 theErr = TRUE;
  77.             }
  78.             else if  (c < 2.0)
  79.             {
  80. /*                theErrorHandler( TOO_FEW_VALUES );*/
  81.                 theErr = TRUE;
  82.             }
  83.             else if  (c > 99.0)
  84.             {
  85. /*                theErrorHandler( TOO_MANY_VALUES );*/
  86.                 theErr = TRUE;
  87.             }
  88.             else
  89.             {
  90.                 gN = (int) floor(c);
  91.             }
  92.         }
  93.         else
  94.         {
  95. /*            goOn = GetLoadedMatrixSize();*/
  96.             if (goOn == FALSE)
  97.             {
  98.                 theErr = TRUE;
  99.             }
  100.         }
  101.         
  102.         if (theErr != TRUE)
  103.         {
  104. /*            InitGlobalPointers();*/
  105.             for (i=1; i<=gN; i++)
  106.             {
  107.                 for (j=1; j<=gN; j++)
  108.                 {
  109.                     siz = (long) sizeof(double);
  110.                     err = FSRead( srcFile, &siz, &c );
  111.                     if (err == noErr)
  112.                     {
  113.                         gA[i][j] = c;
  114.                         gY[i][j] = c;
  115.                     }
  116.                     else
  117.                     {
  118. /*                        fileErrors( err );*/
  119.                         theErr = TRUE;
  120.                         i = gN + 1;
  121.                         j = gN + 1;
  122. /*                        ReleaseGlobalPointers();*/
  123.                     }
  124.                 }
  125.             }
  126.         }
  127.         FSClose( srcFile );
  128.     }
  129.     else if (goOn == TRUE)
  130.     {
  131. /*        fileErrors( err );*/
  132.         theErr = TRUE;
  133.     }
  134.     return (theErr );
  135. }
  136.